{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a08ff89d",
   "metadata": {},
   "source": [
    "# 编译优化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67f5335e",
   "metadata": {},
   "source": [
    "这个模块主要提供拓扑结构适配、量子线路简化等编译优化功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1c4931b",
   "metadata": {},
   "source": [
    "## 1. 拓扑结构适配"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8badd609",
   "metadata": {},
   "source": [
    "在用户进行量子程序设计时，无需关注底层物理量子比特之间的连接关系。Cqlib提供的工具将在执行前完成与目标芯片拓扑结构的适配。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "45df2914",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             ┌──┐   \n",
      " Q0: ───■──────■──M─\n",
      "        │      │    \n",
      " Q1: ───■──■──M┼────\n",
      "           │   │    \n",
      " Q2: ──────■───■──M─\n",
      "             └──┘   \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from cqlib import Circuit\n",
    "from cqlib.visualization import draw_text\n",
    "\n",
    "circuit = Circuit(3)\n",
    "circuit.cz(0, 1)\n",
    "circuit.cz(1, 2)\n",
    "circuit.cz(0, 2)\n",
    "circuit.measure_all()\n",
    "\n",
    "print(draw_text(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c3cdd98",
   "metadata": {},
   "source": [
    "以下以天衍云平台上的物理量子计算机 \"天衍-176-Ⅱ\" 为例："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c83c4d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 gates unfinishednew_circuit: \n",
      "                 ┌──┐   \n",
      " Q13: ───■──■──X───■──M─\n",
      "         │  │  │   │    \n",
      " Q19: ───■──┼──X──M┼────\n",
      "            │      │    \n",
      " Q20: ──────■──────■──M─\n",
      "                 └──┘   \n",
      "\n",
      "\n",
      "初始映射: {0: 19, 1: 13, 2: 20}\n",
      "最终映射: {0: 13, 1: 19, 2: 20}\n"
     ]
    }
   ],
   "source": [
    "from cqlib import TianYanPlatform\n",
    "from cqlib.mapping import transpile_qcis\n",
    "from cqlib.visualization import draw_text\n",
    "\n",
    "# 这里需要将 “连接密钥”替换为用户实际的云平台访问密钥\n",
    "pf = TianYanPlatform(login_key='连接密钥', machine_name='tianyan176-2')\n",
    "new_circuit, initial_layout, swap_mapping, mapping_virtual_to_final = transpile_qcis(circuit.qcis, pf)\n",
    "\n",
    "print(f'new_circuit: \\n{draw_text(new_circuit)}')\n",
    "print(f'初始映射: {initial_layout}\\n'\n",
    "      f'最终映射: {mapping_virtual_to_final}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a50bc2cd",
   "metadata": {},
   "source": [
    "## 2. 线路简化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35ed17d0",
   "metadata": {},
   "source": [
    "Cqlib 内置了若干常见的量子逻辑门优化策略 ，用于合并冗余门操作并简化量子线路。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e0a65fef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原线路：\n",
      "                    \n",
      " Q0: ───X2P──X2M──M─\n",
      "                    \n",
      " Q1: ───X2P───M─────\n",
      "                    \n",
      "\n",
      "\n",
      "简化后的线路：\n",
      "                \n",
      " Q0: ───I(0)──M─\n",
      "                \n",
      " Q1: ───X2P───M─\n",
      "                \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from cqlib.utils import QCIS_Simplify\n",
    "\n",
    "circuit = Circuit(2)\n",
    "circuit.x2p(0)\n",
    "circuit.x2m(0)\n",
    "circuit.x2p(1)\n",
    "circuit.measure_all()\n",
    "print(f'原线路：\\n{draw_text(circuit)}')\n",
    "\n",
    "new_qcis = QCIS_Simplify().simplify(circuit.qcis)\n",
    "print(f'简化后的线路：\\n{draw_text(Circuit.load(new_qcis))}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cqlib3.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
